package com.cn.demo.springboot.redis.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
	@Value("${spring.redis.host}")
	private String host;
	@Value("${spring.redis.password}")
	private String password;
	@Value("${spring.redis.port}")
	private Integer port;
	@Value("${spring.redis.database}")
	private Integer database;
	
	@Bean
	public RedisConnectionFactory jedisConnectionFactory() {
	  //RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration();
	  JedisConnectionFactory connectionFactory =new JedisConnectionFactory();
	  connectionFactory.setDatabase(database);
	  connectionFactory.setHostName(host);
	  connectionFactory.setPassword(password);
	  connectionFactory.setPort(port);
	  return connectionFactory;
	}
	
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {  //
    	StringRedisTemplate redisTemplate = new StringRedisTemplate();
    	redisTemplate.setConnectionFactory(connectionFactory);
    	return redisTemplate;
    }

}
